package com.xcity.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xcity.db.entity.Activity;
import com.xcity.db.entity.Mail;
import com.xcity.game.account.AccountChannel;
import com.xcity.game.activity2.ActivityIds;
import com.xcity.game.activity2.circle.CircleActivity;
import com.xcity.game.activity2.circle.CircleActivityConfig;
import com.xcity.game.activity2.groupbuy.GroupBuyActivity;
import com.xcity.game.activity2.groupbuy.model.ActivityData;
import com.xcity.game.activity2.platform.PlatformGift;
import com.xcity.game.activity2.platform.j9kgames.J9KGamesGiftActivity;
import com.xcity.game.activity2.platform.jg.JGGiftActivity;
import com.xcity.game.activity2.platform.qunhei.QunHeiGiftActivity;
import com.xcity.game.activity2.platform.qzone.QZoneGift;
import com.xcity.game.activity2.platform.qzone.QZoneGiftActivity;
import com.xcity.game.activity2.rechargereward.RechargeRewardActivity;
import com.xcity.game.activity2.share.ShareActivity;
import com.xcity.game.activity2.singlerecharge.SingleRechargeActivity;
import com.xcity.game.http.GMServlet;
import com.xcity.game.mail.MailAttach;

import naga.x.util.CommonUtils;
import naga.x.util.IOUtils;
import naga.x.util.IOUtils.LineReaderListener;
import naga.x.util.NumberUtils;
import naga.x.util.StringUtils;
import naga.x.util.TimeUtils;

public class GMTools {
	
	public static class Server {
		public final int id;
		public final String url;
		public final String key;
		Server(int id, String url, String key) {
			this.id = id;
			this.url = url;
			this.key = key;
		}
	}
	
	public static final Server[] SERVERS = {
			// 欲望都市
			new Server(1, "http://211.159.151.93:3040/xgm", "J1dCf6jfxLejRybSRfuLRtUrSThtj5tf"),
			//old:1, "http://121.41.120.228:3040/xgm", "J1dCf6jfxLejRybSRfuLRtUrSThtj5tf"
//			// 梦幻小镇->欲望都市
//			new Server(2, "http://121.41.120.228:4040/xgm", "HAp9aFzCQIehukSq7D1J8XGnnSdvdVzx"),
//			// 香槟海湾->欲望都市
//			new Server(3, "http://121.41.120.228:5040/xgm", "1R5C5fHkLV4vnneEJG9PqkPNrqkO37cL"),
//			// 国王谷->欲望都市
//			new Server(4, "http://123.206.95.193:3040/xgm", "E06fhw2Hwtj9FOGlCqTBMCaWo71F90MP"),
//			// 法彼雅诺->欲望都市
//			new Server(5, "http://123.206.95.193:4040/xgm", "akyKdJ73A6taDQ4CgE1GtiFHjkrrsJUA"),
//			// 黄金沙漠->欲望都市
//			new Server(6, "http://121.41.120.228:6040/xgm", "3KYVS3yjoxeI9Sp3kJxz4eO62IzwOoid"),
//			// 阿尔卑斯->欲望都市
//			new Server(7, "http://123.206.95.193:5040/xgm", "Pmje6kuRNURJYK9njYOdhRZBzAOcosL7"),
//			// 克利夫兰->欲望都市
//			new Server(8, "http://123.206.95.193:6040/xgm", "UNmFiB76dCItpAmSmOCTaSFqjzXG4GSV"),
//			// 奥克兰
//			new Server(9, "http://123.206.95.193:7040/xgm", "y2rsjT2BMpUdkyD4Lnac5vFlM1NhvCGS"),
//			// 欲望小镇
//			new Server(10, "http://121.41.120.228:7040/xgm", "bco4pMCk050Cy1WwrCt26cCRmiBNoUcN"),
//			// 洛杉矶
//			new Server(11, "http://123.206.95.193:8040/xgm", "OhW5CTEPMBjAESawpbHf7yljd3NFRSq8"),
//			// 香格里拉
//			new Server(12, "http://121.41.120.228:8040/xgm", "cbaDwKA0TZsXAZjs9s4z0Y78nE0a2N13"),
//			// 商战风云->欲望都市
//			new Server(13, "http://123.206.95.193:9040/xgm", "YipCbZsArLRlkeiggfFdaNfsvttL4Oi6"),
//			// 亚特兰大
//			new Server(14, "http://123.206.95.193:1040/xgm", "sdWVMbWavl76wr9KEESrKUSafIu6R9rA"),
			// 圣迭亚哥
//			new Server(15, "http://123.207.166.175:1040/xgm", "uuga9jWCubunh3B5RYTWdPHwPLuGPcO3"),
//			// 夏威夷
//			new Server(16, "http://123.207.166.175:2040/xgm", "vo6K75iAB4Ua1He9rRR63nayD1fGnmTo"),
//			// 爱琴海
//			new Server(17, "http://123.207.166.175:3040/xgm", "NYUXHHXw4zhrPMavYwgOCb1wqXWcTUR2"),
//			// 普罗旺斯
			new Server(18, "http://123.207.166.175:4040/xgm", "ZCGo367Ak4uCmxfxVXKLiOwN7mwIbNWZ"),
			// 圣彼得堡
			new Server(19, "http://123.207.166.175:5040/xgm", "MvYgqR76h5z0HiUqZjBuhTwFcNW2qN51"),
//			// 新几内亚
//			new Server(20, "http://121.41.120.228:9040/xgm", "gPkpqvPl06Rik5LdT86XTwN6xV3ozVWD"),
			// 爱尔兰
			new Server(21, "http://123.207.166.175:6040/xgm", "zN3tUEetzI0cQ0FIrCiknVMAtYsYVyG8"),
			// 澳大利亚
			new Server(22, "http://123.206.95.193:2040/xgm", "hriqGjTLxYYY3cVyGCUM9z4HAwcZ60iS"),
//			// 伊斯坦堡
//			new Server(23, "http://121.41.120.228:1040/xgm", "ykUhJKapCFnWZrdHrX3mWRHcPwlws2OE"),
			// 落基山
			new Server(24, "http://123.207.166.175:7040/xgm", "CeHEGKfGLeIpvkEXKBLZ7nXQr479WqOT"),
			// 苏黎世
			new Server(25, "http://123.206.95.193:3040/xgm", "CgPAyKNIUqM3GEOmrL2m2TPcWv67tO4n"),
			// 威尼斯
			new Server(26, "http://123.206.95.193:4040/xgm", "nIrYGv1k1ToLYvqaDqvbR2ghIbByCMvx"),
			// 因特拉肯
			new Server(27, "http://123.206.95.193:5040/xgm", "z7soNJSbiBTXzFXsMwZf2iTLkFxD7anu"),
	};
	
	static Server TEST_SERVER = new Server(1001, "http://121.41.120.228:28888/xgm", "xZSpvVOseohoWZ6KC6hVNEtncXSrtd9s");
	static Server LOCAL_SERVER = new Server(5002, "http://127.0.0.1:3040/xgm", "GYRFIBzzvRRIrAaAiArgaQeo7j2mp93m");
	static Server SWITCH_SERVER = new Server(1, "http://211.159.151.93:3040/xgm", "J1dCf6jfxLejRybSRfuLRtUrSThtj5tf");
	
	public static void main(String[] args) {
//		String s = "尊敬的各位玩家，服务器将于16:00进行停服维护，届时将无法登录游戏，预计17:00恢复正常。";
//		for (final Server server : SERVERS) {
//			doNotice(server, s);
//		}
//		doNotice(SERVERS[1], s);
//		doNotice(SERVERS[2], s);
//		doNotice(SERVERS[3], s);
//		doNotice(SERVERS[4], s);

//		MailAttach attach = new MailAttach();
////		20033、20030
//		attach.addStone(500);
////		attach.addItem(61030004, 12200);
//		Mail mail = new Mail(Mail.TYPE_SYSTEM, -1, 313532632288L,
//				"星级达人卡补偿",
//				"星级达人卡补偿",
//				attach);
//		doSendMail(SWITCH_SERVER, mail);
		
//		batchSendMail();
//		doPatch(SERVERS[0], "patch.CheckPlayerBuildPatch009");

//		updateActivity(TEST_SERVER, newGroupBuyActivity(20170908, "2017-09-08 12:00:00", "2017-09-14 23:59:00", "2017-09-14 23:59:00"));
//		updateActivity(TEST_SERVER, newSingleRechargeActivity(20170908, "2017-09-08 12:00:00", "2017-09-13 23:59:00", "2017-09-13 23:59:00"));
		
//		for (final Server server : SERVERS) {
////			doPatch(SERVERS[10], "patch.NanGongXiaoYanArenaPatch");
////			doPatch(SERVERS[15], "patch.MineCheckPatch2");
//			doPatch(server, "patch.ReloadShopPatch001");
//		}
		for (Server server : SERVERS) {
//		for (Server server : Arrays.asList(TEST_SERVER)) {
//			hotfix(server, "com.xcity.game.combat.skill.impl.COMBAT_SKILL_90122$BUFF");
//			hotfix(server, "com.xcity.game.combat.skill.impl.COMBAT_SKILL_90131$BUFF");
//			hotfix(server, "com.xcity.game.equip.suit4.impl.Suit4Cartire");
//			//7月28日16:00-8月2日23:59
//			updateActivity(server, newSingleRechargeActivity(20170728, "2017-07-28 16:00:00", "2017-08-02 23:59:00", "2017-08-02 23:59:00"));
//			//团购：7月28日16:00-8月3日23:59
//			updateActivity(server, newGroupBuyActivity(20170728, "2017-07-28 16:00:00", "2017-08-03 23:59:00", "2017-08-03 23:59:00"));
//			hotfix(server, "com.xcity.game.peakroad.model.PeakRoadInstance");
//			hotfix(server, "com.xcity.game.activity2.platform.j9kgames.J9KGamesGiftActivity");
//			hotfix(server, "com.xcity.game.activity2.platform.jg.JGGiftActivity");
//			hotfix(server, "com.xcity.game.activity2.platform.qunhei.QunHeiGiftActivity");
//			hotfix(server, "com.xcity.game.activity2.platform.qzone.QZoneGiftActivity");
//			hotfix(server, "com.xcity.game.mail.MailAttach");
//			updateActivity(server, newGroupBuyActivity(20170922, "2017-09-22 16:00:00", "2017-09-28 23:59:00", "2017-09-28 23:59:00"));
//			updateActivity(server, newSingleRechargeActivity(20170922, "2017-09-22 16:00:00", "2017-09-27 23:59:00", "2017-09-27 23:59:00"));
//			doPatch(server, "patch.FixCumulativeActivityPatch015");
		}
		
//		//--------新服活动开始
//		// 活动
//		addActivity(SERVERS[SERVERS.length - 1], newQZoneGiftActivity());
//		addActivity(SERVERS[SERVERS.length - 1], new99KGamesGiftActivity());
//		addActivity(SERVERS[SERVERS.length - 1], new9GGiftActivity());
//		addActivity(SERVERS[SERVERS.length - 1], newQunHeiActivity());
//		addActivity(SERVERS[SERVERS.length - 1], newShareActivity());
//		addActivity(SERVERS[SERVERS.length - 1], newRechargeRewardActivity());
//		//--------新服活动结束
//		addActivity(SERVERS[SERVERS.length - 1], newGroupBuyActivity(20170922, "2017-09-22 16:00:00", "2017-09-28 23:59:00", "2017-09-28 23:59:00"));
//		addActivity(SERVERS[SERVERS.length - 1], newSingleRechargeActivity(20170922, "2017-09-22 16:00:00", "2017-09-27 23:59:00", "2017-09-27 23:59:00"));
		
//		addActivity(SERVERS[SERVERS.length - 1], newGroupBuyActivity(20170728, "2017-07-28 16:00:00", "2017-08-03 23:59:00", "2017-08-03 23:59:00"));
//		addActivity(SERVERS[SERVERS.length - 1], newSingleRechargeActivity(20170728, "2017-07-28 16:00:00", "2017-08-02 23:59:00", "2017-08-02 23:59:00"));

		// 活动开始时间
		String beginTime = "2017-09-01 00:00:00";
		// 活动结束时间
		String endTime = "2017-09-07 23:59:59";
		// 排行榜开启时间
		String rankingOpenTime = "2017-08-25 00:00:00";
		// 排行榜关闭时间
		String rankingCloseTime = "2017-08-32 00:02:00";
		// 商店开启时间
		String shopOpenTime = "2017-09-01 00:00:00";
		// 商店关闭时间
		String shopCloseTime = "2017-09-07 23:59:59";
		// 新服时间范围（在此期间，数据取对应的新服数据）
		String serverNewBeginTime = "2017-08-18 00:00:00";
		String serverNewEndTime = "2017-08-24 23:59:59";
		
		Activity act = newCircleActivity(beginTime, endTime, rankingOpenTime, rankingCloseTime, shopOpenTime, shopCloseTime, serverNewBeginTime, serverNewEndTime);
//		updateActivity(TEST_SERVER, act);
		
//		for(int i = 0; i < SERVERS.length; i++){
////			updateActivity(SERVERS[i], act);
////			updateActivity(SERVERS[i], newGroupBuyActivity(20170922, "2017-09-22 16:00:00", "2017-09-28 23:59:00", "2017-09-28 23:59:00"));
////			updateActivity(SERVERS[i], newSingleRechargeActivity(20170922, "2017-09-22 16:00:00", "2017-09-27 23:59:00", "2017-09-27 23:59:00"));
//		}
//		addActivity(SERVERS[SERVERS.length - 1], newRechargeRewardActivity());
//		updateActivity(TEST_SERVER, newRechargeRewardActivity());
//		addActivity(SERVERS[SERVERS.length - 1], act);
		
//		addActivity(TEST_SERVER, newRechargeRewardActivity());
		
//		batchSendMail();
	}
	
	public static Activity newSingleRechargeActivity(int serial, String beginTime, String rechargeEndTime, String endTime) {
		Activity act = new Activity();
		act.setUniqueId(ActivityIds.ACTIVITY_SINGLE_RECHARGE);
		act.setName("单笔充值");
		act.setImplClass(SingleRechargeActivity.class.getName());
		act.setBeginTime(TimeUtils.parse(beginTime));
		act.setEndTime(TimeUtils.parse(endTime));
		com.xcity.game.activity2.singlerecharge.model.ActivityData data = new com.xcity.game.activity2.singlerecharge.model.ActivityData();
		data.serial = serial;
		data.rechargeEndTime = TimeUtils.parse(rechargeEndTime).getTime();
		act.setData(JSON.toJSONString(data));
		return act;
	}
	
	public static Activity newGroupBuyActivity(int serial, String beginTime, String buyEndTime, String endTime) {
		Activity act = new Activity();
		act.setUniqueId(ActivityIds.ACTIVITY_GROUP_BUY);
		act.setName("限时团购");
		act.setImplClass(GroupBuyActivity.class.getName());
		act.setBeginTime(TimeUtils.parse(beginTime));
		act.setEndTime(TimeUtils.parse(endTime));
		ActivityData data = new ActivityData();
		data.serial = serial;
		data.buyEndTime = TimeUtils.parse(buyEndTime).getTime();
		act.setData(JSON.toJSONString(data));
		return act;
	}
	
	public static Activity newShareActivity() {
		Activity act = new Activity();
		act.setUniqueId(20170620);
		act.setName("分享活动");
		act.setDesc("玩吧、微博分享");
		act.setImplClass(ShareActivity.class.getName());
		return act;
	}
	
	// 欢乐转盘
	public static Activity newCircleActivity(String beginTime, String endTime,
											String rankingOpenTime, String rankingCloseTime,
											String shopOpenTime, String shopCloseTime,
											String serverNewBeginTime, String serverNewEndTime) {
		Activity act = new Activity();
		act.setUniqueId(ActivityIds.ACTIVITY_CIRCLE);
		act.setName("欢乐转盘");
		act.setDesc("");
		act.setImplClass(CircleActivity.class.getName());
		act.setBeginTime(TimeUtils.parse(beginTime));
		act.setEndTime(TimeUtils.parse(endTime));
		act.setData(JSON.toJSONString(newCircleActivityConfig(rankingOpenTime, rankingCloseTime, shopOpenTime, shopCloseTime, serverNewBeginTime, serverNewEndTime)));
		return act;
	}
	
	public static CircleActivityConfig newCircleActivityConfig(String rankingOpenTime, String rankingCloseTime,
															String shopOpenTime, String shopCloseTime,
															String serverNewBeginTime, String serverNewEndTime) {
		CircleActivityConfig config = new CircleActivityConfig();
		config.rankingOpenTime = TimeUtils.parse(rankingOpenTime).getTime();
		config.rankingCloseTime = TimeUtils.parse(rankingCloseTime).getTime();
		config.shopOpenTime = TimeUtils.parse(shopOpenTime).getTime();
		config.shopCloseTime = TimeUtils.parse(shopCloseTime).getTime();
		config.serverNewBeginTime = TimeUtils.parse(serverNewBeginTime).getTime();
		config.serverNewEndTime = TimeUtils.parse(serverNewEndTime).getTime();
		return config;
	}

	public static Activity newRechargeRewardActivity() {
		Activity act = new Activity();
		act.setUniqueId(ActivityIds.ACTIVITY_RECHARGE_REWARD);
		act.setName("充值送员工活动");
		act.setDesc("");
		act.setImplClass(RechargeRewardActivity.class.getName());
		act.setBeginTime(TimeUtils.parse("2007-07-13 23:59:59"));
		act.setEndTime(TimeUtils.parse("2057-07-13 23:59:59"));
		return act;
	}
	
	public static void batchSendMail() {
		MailAttach attach = new MailAttach();
//		20033、20030
//		attach.addItem(20033, 1);
//		attach.addItem(80230, 1);
		attach.addStone(500);//TODO batchSendMail
		attach.addGold(50000);
		Mail mail = new Mail(Mail.TYPE_SYSTEM, -1, -1,
				"9月15日临时停服补偿",//TODO batchSendMail
				"由9月15日临时停服造成的不便将统一进行补偿。感谢广大玩家的支持与厚爱！",//TODO batchSendMail
				attach);
		
		//TODO batchSendMail
		File baseDir = new File("D:/temp/buchang/2017_09_18");//C:/Users/yang.li/Desktop/server_release/v20170728/补偿
		try {
			System.setOut(new PrintStream(new File(baseDir, "stdout.log")));//
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		final Set<Long> ids = new HashSet<>(2048);
//		Server[] TESTS = new Server[]{TEST_SERVER};
		boolean needReadStd = false;//TODO batchSendMail
		for (final Server server : SERVERS) {////TODO
			File file = new File(baseDir, server.id + ".txt");//"test"//TODO batchSendMail
			final AtomicInteger counter = new AtomicInteger();
			IOUtils.read(file, IOUtils.UTF8, new LineReaderListener() {
				@Override
				public boolean readLine(String line) {
					if(needReadStd){
						int from = line.indexOf("ID[");
						if (from == -1) {
							return true;
						}
						from += "ID[".length();
						int to = line.indexOf(']', from);
						line = line.substring(from, to);
					}
					if(line.equals("id")){
						return true;
					}
					
					long pid = NumberUtils.parseLong(line);
					if (pid > 0) {
						if (!ids.contains(pid)) {
							ids.add(pid);
							counter.incrementAndGet();
							mail.setDestId(pid);
							doSendMail(server, mail);
						}
					} else {
						System.out.println("!!!pid error:" + line);
					}
					return true;
				}
			});
			System.out.println("mail ok, server " + server.id + ":" + counter.get());
		}
	}
	
	public static Activity newQZoneGiftActivity() {
		Activity act = new Activity();
		act.setUniqueId(201705261);
		act.setName("玩吧登录礼包");
		act.setDesc("");
		act.setImplClass(QZoneGiftActivity.class.getName());
		act.setData(JSON.toJSONString(newQZoneGifts()));
		return act;
	}
	
	public static Activity new99KGamesGiftActivity() {
		Activity act = new Activity();
		act.setUniqueId(201705262);
		act.setName("游戏金榜关注礼包");
		act.setDesc("");
		act.setImplClass(J9KGamesGiftActivity.class.getName());
		act.setData(JSON.toJSONString(new99KGamesGift()));
		return act;
	}
	
	public static Activity new9GGiftActivity() {
		Activity act = new Activity();
		act.setUniqueId(201705263);
		act.setName("9G关注礼包");
		act.setDesc("");
		act.setImplClass(JGGiftActivity.class.getName());
		act.setData(JSON.toJSONString(new9GGift()));
		return act;
	}
	
	public static Activity newQunHeiActivity() {
		Activity act = new Activity();
		act.setUniqueId(201706021);
		act.setName("群黑游戏分享礼包");
		act.setDesc("");
		act.setImplClass(QunHeiGiftActivity.class.getName());
		act.setData(JSON.toJSONString(newQunHeiGift()));
		return act;
	}
	
	public static PlatformGift new99KGamesGift() {
		PlatformGift gift = new PlatformGift();
		gift.type = PlatformGift.TYPE_FOLLOW;
		gift.channel = AccountChannel.J9KGAMES;
		gift.id = 20170526;
		gift.content = new MailAttach();
//		礼包内容：50钻、50万游戏币、5个AA碎片
//		物品ID：20008、20024、60034*5
		gift.content.addItem(20008, 1);
		gift.content.addItem(20024, 1);
		gift.content.addItem(60034, 5);
		return gift;
	}
	
	public static PlatformGift new9GGift() {
		PlatformGift gift = new PlatformGift();
		gift.type = PlatformGift.TYPE_FOLLOW;
		gift.channel = AccountChannel.JG;
		gift.id = 20170526;
		gift.content = new MailAttach();
//		礼包内容：50钻、50万游戏币、5个AA碎片
//		物品ID：20008、20024、60034*5
		gift.content.addItem(20008, 1);
		gift.content.addItem(20024, 1);
		gift.content.addItem(60034, 5);
		return gift;
	}
	
	public static List<QZoneGift> newQZoneGifts() {
		List<QZoneGift> list = new ArrayList<>(7);
		QZoneGift qz = new QZoneGift();
		qz.type = PlatformGift.TYPE_LOGIN;
		qz.channel = AccountChannel.QQ_WANBA;
//		qz.calFrom = getCalendar("2017-05-21 21:00:00");
//		qz.calTo = getCalendar("2017-05-22 21:00:00");
//		周一礼包
//		草莓x1、AA员工碎片x10
//		20028	1	60034	10
		qz.id = 901;
		qz.content = new MailAttach();
		qz.content.addItem(20028, 1);
		qz.content.addItem(60034, 10);
		list.add(qz);
//		周二礼包
//		量立方x1、中级奢侈品礼盒
//		30109	1	30102	1
		qz = new QZoneGift();
		qz.type = PlatformGift.TYPE_LOGIN;
		qz.channel = AccountChannel.QQ_WANBA;
//		qz.calFrom = getCalendar("2017-05-22 21:00:00");
//		qz.calTo = getCalendar("2017-05-23 21:00:00");
		qz.id = 902;
		qz.content = new MailAttach();
		qz.content.addItem(30109, 1);
		qz.content.addItem(30102, 1);
		list.add(qz);
//		周三礼包
//		50万现金、高级简历x1
//		20024	1	30083	1
		qz = new QZoneGift();
		qz.type = PlatformGift.TYPE_LOGIN;
		qz.channel = AccountChannel.QQ_WANBA;
//		qz.calFrom = getCalendar("2017-05-23 21:00:00");
//		qz.calTo = getCalendar("2017-05-24 21:00:00");
		qz.id = 903;
		qz.content = new MailAttach();
		qz.content.addItem(20024, 1);
		qz.content.addItem(30083, 1);
		list.add(qz);
//		周四礼包
//		50万现金、华美建材箱x3
//		20024	1	30307	3
		qz = new QZoneGift();
		qz.type = PlatformGift.TYPE_LOGIN;
		qz.channel = AccountChannel.QQ_WANBA;
//		qz.calFrom = getCalendar("2017-05-24 21:00:00");
//		qz.calTo = getCalendar("2017-05-25 21:00:00");
		qz.id = 904;
		qz.content = new MailAttach();
		qz.content.addItem(20024, 1);
		qz.content.addItem(30307, 1);
		list.add(qz);
//		周五礼包
//		香肠x1、黄金经验奖章x5
//		20030	1	61020105	5
		qz = new QZoneGift();
		qz.type = PlatformGift.TYPE_LOGIN;
		qz.channel = AccountChannel.QQ_WANBA;
//		qz.calFrom = getCalendar("2017-05-25 21:00:00");
//		qz.calTo = getCalendar("2017-05-26 21:00:00");
		qz.id = 905;
		qz.content = new MailAttach();
		qz.content.addItem(20030, 1);
		qz.content.addItem(61020105, 5);
		list.add(qz);
//		周六礼包
//		星级达人卡x10、品味潮人卡x10
//		61030004	10	61030003	10
		qz = new QZoneGift();
		qz.type = PlatformGift.TYPE_LOGIN;
		qz.channel = AccountChannel.QQ_WANBA;
//		qz.calFrom = getCalendar("2017-05-26 21:00:00");
//		qz.calTo = getCalendar("2017-05-27 21:00:00");
		qz.id = 906;
		qz.content = new MailAttach();
		qz.content.addItem(61030004, 10);
		qz.content.addItem(61030003, 10);
		list.add(qz);
//		周日礼包
//		100万现金、钻石x100
//		20031	1	20009	1
		qz = new QZoneGift();
		qz.type = PlatformGift.TYPE_LOGIN;
		qz.channel = AccountChannel.QQ_WANBA;
//		qz.calFrom = getCalendar("2017-05-27 21:00:00");
//		qz.calTo = getCalendar("2017-05-28 21:00:00");
		qz.id = 907;
		qz.content = new MailAttach();
		qz.content.addItem(20031, 1);
		qz.content.addItem(20009, 1);
		list.add(qz);
		return list;
	}
	
	public static PlatformGift newQunHeiGift() {
		PlatformGift gift = new PlatformGift();
		gift.type = PlatformGift.TYPE_SHARE;
		gift.channel = AccountChannel.QUNHEI;
		gift.id = 201706021;
		gift.content = new MailAttach();
//		礼包内容：50钻、50万游戏币、5个AA碎片
//		物品ID：20008、20024、60034*5
		gift.content.addItem(20008, 1);
		gift.content.addItem(20024, 1);
		gift.content.addItem(60034, 5);
		return gift;
	}
	
	public static void hotfix(Server server, String className) {
		post(GMServlet.CMD_HOTFIX, server.url, server.key, className);
	}
	
	public static void doPatch(Server server, String className) {
		post(GMServlet.CMD_PATCH, server.url, server.key, className);
	}
	
	public static void doSendMail(Server server, Mail mail) {
		post(GMServlet.CMD_SEND_MAIL, server.url, server.key, JSON.toJSONString(mail), true);
	}
	
	public static void doNotice(Server server, String msg) {
		post(GMServlet.CMD_NOTICE, server.url, server.key, msg, true);
	}
	
	public static void addActivity(Server server, Activity activity) {
		post(GMServlet.CMD_ADD_ACTIVITY, server.url, server.key, JSON.toJSONString(activity), true);
	}
	
	public static void removeActivity(Server server, int id) {
		post(GMServlet.CMD_DELETE_ACTIVITY, server.url, server.key, String.valueOf(id));
	}
	
	public static void updateActivity(Server server, Activity activity) {
		post(GMServlet.CMD_UPDATE_ACTIVITY, server.url, server.key, JSON.toJSONString(activity), true);
	}
	
	public static void queryActivity(Server server, int id) {
		post(GMServlet.CMD_QUERY_ACTIVITY, server.url, server.key, String.valueOf(id));
	}
	
	public static void queryActivities(Server server) {
		post(GMServlet.CMD_QUERY_ACTIVITY, server.url, server.key, "");
	}
	
	public static void addForbid(Server server, long id, int minutes) {
		JSONObject json = new JSONObject();
		json.put("action", "add");
		json.put("id", id);
		json.put("time", minutes);
		post(GMServlet.CMD_FORBID, server.url, server.key, JSON.toJSONString(json), true);
	}
	
	public static void removeForbid(Server server, long id) {
		JSONObject json = new JSONObject();
		json.put("action", "remove");
		json.put("id", id);
		post(GMServlet.CMD_FORBID, server.url, server.key, JSON.toJSONString(json), true);
	}
	
	public static void viewForbid(Server server) {
		JSONObject json = new JSONObject();
		json.put("action", "view");
		post(GMServlet.CMD_FORBID, server.url, server.key, JSON.toJSONString(json), true);
	}
	
	static void post(String cmd, String url, String key, String data) {
		post(cmd, url, key, data, false);
	}
	
	static void post(String cmd, String url, String key, String data, boolean base64) {
		if (base64) {
			byte[] b = null;
			b = Base64.getUrlEncoder().encode(StringUtils.stringToBytes(data, IOUtils.UTF8));
			data = StringUtils.bytesToString(b, IOUtils.UTF8);
		}
		String[] params = new String[] {
				"cmd", cmd,
				"data", data,
				"sign", ""
		};
		String sign = makeSign(params, key);
		params[params.length - 1] = sign;
		StringBuilder buf = new StringBuilder(url);
		for (int i = 0; i < params.length; i += 2) {
			buf.append(i == 0 ? '?' : '&');
			buf.append(params[i]).append('=').append(params[i + 1]);
		}
		String s = buf.toString();
		System.out.println(s);
		s = HttpUtils.post(url, params);
		System.out.println(s);
	}
	
	static String makeSign(String[] params, String key) {
		StringBuilder buf = new StringBuilder();
		for (int i = 0; i < params.length; i += 2) {
			if (!"sign".equals(params[i])) {
				buf.append(params[i]).append("=").append(params[i + 1]).append("&");
			}
		}
		buf.append("key=" + key);
		return CommonUtils.md5(buf.toString());
	}
	
}
